WRANGLERS
Photo by Imre Tömösvári on Unsplash
Man is not the sum of what he has already, but rather the sum of what he does not yet have, of what he could have….
— Jean-Paul Sartre
df <- read_csv('./archetypes/MissingMigrants-Global-2020-10-18T06-37-33.csv')
df <- df %>%
mutate_if(is.numeric, ~replace(., is.na(.), 0))
df_1 <- df %>%
group_by(`Reported Year`) %>%
summarise(sum_number=sum(`Total Dead and Missing`))
df_2<- df %>%
group_by(`Reported Year`, `Reported Month`) %>%
summarise(sum_number=sum(`Total Dead and Missing`))
df_2$`Reported Month` <- factor(df_2$`Reported Month`, levels=c('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'))
df_2$`Reported Year` <- factor(df_2$`Reported Year`, levels=c('2014','2015','2016','2017','2018','2019','2020'))
year_palette <- c("2014" = "#E9EAF6", "2015" = "#C5C8E0", "2016" = "#959DCA", "2017" = "#4764A8", "2018" = "#6F7DB6",
"2019" = "#33B5B3", "2020" = "#C5C8E0")
v1 <- ggplot(df_2,aes(x=`Reported Month`, y=sum_number, fill=`Reported Year`)) +
geom_hline(yintercept = 0, size=0.1, color = "grey") +
geom_hline(yintercept = 500, size=0.1, color = "grey") +
geom_hline(yintercept = 1000, size=0.1, color = "grey") +
geom_hline(yintercept = 1500, size=0.1, color = "grey") +
geom_bar(stat="identity", position=position_dodge()) +
scale_fill_manual(values=year_palette, , name = "YEAR") +
geom_text(aes(label = sum_number), hjust=-0.1, size = 3, angle=90,
position = position_dodge(0.9)) +
theme_tufte(base_size = 15) +
theme(
panel.background = element_blank(),
plot.title = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.ticks = element_blank()
#plot.margin = unit(c(1, 5, 1, 1), "lines")
) +
theme(legend.position="top") +
guides(colour = guide_legend(nrow = 1))
girafe(ggobj = v1, width_svg = 1280/72, height_svg = 720/72, options =
list(opts_sizing(rescale = TRUE, width = 1.0)))
df_3 <- df_2 %>%
pivot_wider(names_from = `Reported Month`, values_from = sum_number) %>%
relocate(`Reported Year`,Jan , Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec)
df_4 <- df_3 %>%
pivot_longer(!`Reported Year`,names_to = "MONTH", values_to = "TOTAL_DEAD")
####################################################################################
# Required libraries
library(dplyr)
library(lazyeval)
####################################################################################
# Group More than One variable and Perform Calculation
f_groupby_sum <- function(data,column1_gr, column2_gr, column_sum) {
data %>%
group_by_(as.name(column1_gr), as.name(column2_gr)) %>%
summarise_(SUM_COLUMN = interp(~sum(var, na.rm=T), var = as.name(column_sum)))
}
# Call the function passing your own arguments
f_groupby_sum(df, "Reported Year","Reported Month","Total Dead and Missing")
####################################################################################
####################################################################################
# Unpivot a table
f_pivot <- function(data, cole, ...) {
data %>%
pivot_wider(names_from = cole, values_from = sum_number) %>%
relocate(all_of(...),Jan , Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec)
}
# Call the function
f_pivot(df_2, 'Reported Month', 'Reported Year')
####################################################################################
####################################################################################
# Unpivot a table
f_unpivot <- function(data,...) {
data %>%
pivot_longer(!all_of(...),names_to = "MONTH", values_to = "TOTAL_DEAD")
}
# Call the function
f_unpivot(df_3, 'Reported Year')
####################################################################################
## [1] MissingMigrants. _Missing Migrants Project_. Iom.int, Jun. 2001.
## <URL: https://missingmigrants.iom.int/> (visited on 06/08/2021).